package com.spkj.srb.core.controller.admin;


import com.spkj.srb.common.exception.Assert;
import com.spkj.srb.common.exception.BusinessException;
import com.spkj.srb.common.result.ResponseEnum;
import com.spkj.srb.core.pojo.entity.IntegralGrade;
import com.spkj.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spkj.srb.common.result.R;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author spkj
 * @since 2022-06-12
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@CrossOrigin
@Api(tags = "积分等级管理")
public class AdminIntegralGradeController {
    @Autowired
    private IntegralGradeService integralGradeService;

    @GetMapping("/listAll")
    @ApiOperation(value = "积分等级列表")
    public R listAll(HttpServletRequest request){
//        int i = 1 / 0;
        String remoteAddr = request.getRemoteAddr();
        System.out.println("remoteAddr = " + remoteAddr);
        String header = request.getHeader("X-forwarded-for");
        System.out.println("header = " + header);

        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "根据id删除积分等级")
    public R remove(@ApiParam(value = "数据id", required = true) @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @PostMapping("/save")
    @ApiOperation("新增积分等级")
    public R save(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
//        if (borrowAmount == null){
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(borrowAmount, ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

//        if (borrowAmount.compareTo(new BigDecimal(0)) != 1){
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_ZERO_ERROR);
//        }
        Assert.isTrue(borrowAmount.compareTo(new BigDecimal(0)) == 1, ResponseEnum.BORROW_AMOUNT_ZERO_ERROR);

        boolean save = integralGradeService.save(integralGrade);
        if (save){
            return R.ok().message("保存成功");
        }
        return R.error().message("保存失败");
    }

    @GetMapping("/get/{id}")
    @ApiOperation("根据id获取积分等级")
    public R getById(@ApiParam(value = "数据id", required = true) @PathVariable Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null){
            return R.ok().data("record", integralGrade);
        }
        return R.error().message("数据不存在");
    }

    @PutMapping("/update")
    @ApiOperation("更新积分等级")
    public R update(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade){
        boolean update = integralGradeService.updateById(integralGrade);
        if (update){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

}

